const dao = require('./base');

module.exports = {

    bancorPriceList: function (condition, fields = "*", by = []) {
        let sql = `SELECT ${fields} from ex_bancor_price a where 1=1`;
        let pre = [];
        if (condition.pid !== undefined && condition.pid !== '') {
            sql += ` and a.pid = ?`;
            pre.push(condition.pid);
        }
        if (condition.contract !== undefined && condition.contract !== '') {
            sql += ` and a.contract = ?`;
            pre.push(condition.contract);
        }
        sql = dao.by(sql, by);
        let limit = dao.limit(sql, condition, pre);
        sql = limit.sql;
        pre = limit.pre;
        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            throw new Error(err + " sql：" + sql + " pre：" + pre);
        });
    },

    bancorPriceAdd: function (fields = {}) {
        let sql = "INSERT INTO `ex_bancor_price` (`contract`, `price`) VALUES " +
            "(?,?)";
        let pre = [];
        pre.push(fields.contract);
        pre.push(fields.price);
        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            throw new Error(err + " sql：" + sql + " pre：" + pre);
        });
    },

    bancorPriceUpdate: async function (condition = {}, fields = {}) {
        let sql = "update ex_bancor_price set ";
        let pre = [];
        if (fields.price !== undefined && fields.price !== '') {
            sql = dao.update_sql_splice(sql, "price");
            pre.push(fields.price);
        }
        // if (fields.del_flag !== undefined && fields.del_flag !== '') {
        //     sql = dao.update_sql_splice(sql, "del_flag");
        //     pre.push(fields.del_flag);
        // }
        if (condition.contract !== undefined && condition.contract !== '') {
            sql += ` where contract = ?`;
            pre.push(condition.contract);
        } else {
            throw new Error("condition is null");
        }

        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            throw new Error(err);
        });
    },


};
